﻿using ComLib.ConfigService;
using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PassportReaderAPI
{
    public class PassportReaderOption : IniConfigBase
    {
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        public override string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    string className = this.GetType().Name;
                    _strConfigPath = Path.Combine(ConfigDirectory, className + ".ini");
                }
                return _strConfigPath;
            }
        }

        /// <summary>
        /// 护照证件阅读识别类型ID
        /// </summary>
        private int _passportTypeId = 13;
        public int PassportTypeId
        {
            get { return _passportTypeId; }
            set { _passportTypeId = value; }
        }

        /// <summary>
        /// 签证阅读识别类型ID
        /// </summary>
        private int _visaTypeId = 12;
        public int VisaTypeId
        {
            get { return _visaTypeId; }
            set { _visaTypeId = value; }
        }

        /// <summary>
        /// 读本失败的重试次数
        /// </summary>
        private int _retryTimes = 3;
        public int RetryTimes
        {
            get { return _retryTimes; }
            set { _retryTimes = value; }
        }

        /// <summary>
        /// 阅读器主动扫描间隔时间（毫秒）
        /// </summary>
        private int _readInterval = 1200;
        public int ReadInterval
        {
            get { return _readInterval; }
            set { _readInterval = value; }
        }

        /// <summary>
        /// 阅读器配置ID
        /// </summary>
        private string _userId = "66915733240631852974";
        public string UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        /// <summary>
        /// DLL目录
        /// </summary>
        private string _dllDir = @"C:\Program Files (x86)\Passport Reader\lib";
        public string DllDir
        {
            get { return _dllDir; }
            set { _dllDir = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("PassportTypeId", PassportTypeId);
            SaveField("VisaTypeId", VisaTypeId);
            SaveField("RetryTimes", RetryTimes);
            SaveField("ReadInterval", ReadInterval);
            SaveField("UserId", _userId);
            SaveField("DllDir", _dllDir);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _passportTypeId = LoadInt32Value("PassportTypeId");
                _visaTypeId = LoadInt32Value("VisaTypeId");
                _retryTimes = LoadInt32Value("RetryTimes");
                _readInterval = LoadInt32Value("ReadInterval");
                _userId = LoadStringValue("UserId");
                _dllDir = LoadStringValue("DllDir");
            }
            catch (Exception ex)
            {
                string strMsg = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("刷身份证选项: ");
            builder.Append("[护照证件阅读识别类型ID]:" + _passportTypeId.ToString());
            builder.Append("; [签证阅读识别类型ID]:" + _visaTypeId.ToString());
            builder.Append("; [读本失败的重试次数]:" + _retryTimes.ToString());
            builder.Append("; [阅读器主动扫描间隔时间]:" + _readInterval.ToString());
            builder.Append("; [UserId]:" + UserId);
            builder.Append("; [DLL目录]:" + DllDir);
            return builder.ToString();
        }

    }
}
